package com.example.mcodebox;
/*
 * @autor at033
 * time:2014-06-06
 * 
 */
import android.content.ContentValues;
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v7.app.ActionBarActivity;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

import com.example.database.DBOpenHelper;

public class AddActivity extends ActionBarActivity {
	private EditText editCompany;
	private EditText editUsername;
	private EditText editPwd1;
	private EditText editPwd2;
	private EditText editMark;
	private Button confirm_btn;
	private Button cancle_btn;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.fragment_add);

		editCompany = (EditText) findViewById(R.id.Service_providers_ET);
		editUsername = (EditText) findViewById(R.id.Account_number_ET);
		editPwd1 = (EditText) findViewById(R.id.Pwd_ET);
		editPwd2 = (EditText) findViewById(R.id.confirm_pwd_ET);
		editMark = (EditText) findViewById(R.id.description_ET);
		confirm_btn = (Button) findViewById(R.id.add_ok_btn);

		confirm_btn.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				// 将输入的数据获取到
				String company = editCompany.getText().toString();
				String account = editUsername.getText().toString();
				String password1 = editPwd1.getText().toString();
				String password2 = editPwd2.getText().toString();
				String mark = editMark.getText().toString();

				System.out.println("----获取当前用户输入的值-------------");
				System.out.println("company:" + company + "\n account:"
						+ account + "\n password1:" + password1 + "\n mark:"
						+ mark);
				System.out.println("----当前用户的值---------------------");

				if (password1 != null && password1.equals(password2)) {

					ContentValues values = new ContentValues();
					// 在values中添加信息
					values.put("company", company);
					values.put("account", account);
					values.put("password", password1);
					values.put("mark", mark);
					// 实例化数据库帮助类
					DBOpenHelper helper = new DBOpenHelper(
							getApplicationContext());
					// 插入数据
					helper.insert(values);
					System.out.println("插入完成！");
					Toast.makeText(AddActivity.this, "插入成功！",
							Toast.LENGTH_SHORT).show();
					// 插入成功后将输入框中设为空！
					editCompany.setText(null);
					editMark.setText(null);
					editPwd1.setText(null);
					editPwd2.setText(null);
					editUsername.setText(null);

					// 实例化Intent
					Intent intent = new Intent(AddActivity.this,
							ShowActivity.class);
					// 启动Activity
					startActivity(intent);
				} else {
					Toast.makeText(AddActivity.this, "两次输入的密码不一致",
							Toast.LENGTH_SHORT).show();
					editPwd1.setText(null);// 将密码框中的字清空
					editPwd2.setText(null);
				}
			}
		});
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {

		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.add, menu);
		return true;
	}

	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		// Handle action bar item clicks here. The action bar will
		// automatically handle clicks on the Home/Up button, so long
		// as you specify a parent activity in AndroidManifest.xml.
		int id = item.getItemId();
		if (id == R.id.action_settings) {
			return true;
		}
		return super.onOptionsItemSelected(item);
	}

	/**
	 * A placeholder fragment containing a simple view.
	 */
	public static class PlaceholderFragment extends Fragment {

		public PlaceholderFragment() {
		}

		@Override
		public View onCreateView(LayoutInflater inflater, ViewGroup container,
				Bundle savedInstanceState) {
			View rootView = inflater.inflate(R.layout.fragment_add, container,
					false);
			return rootView;
		}
	}

}
